package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.UacStaffStatusMapper;
import com.csun.cmny.provider.model.domain.UacStaff;
import com.csun.cmny.provider.model.domain.UacStaffStatus;
import com.csun.cmny.provider.model.dto.staff.StaffStatusQueryDto;
import com.csun.cmny.provider.model.exceptions.UacBizException;
import com.csun.cmny.provider.model.vo.StaffStatusVo;
import com.csun.cmny.provider.service.UacStaffService;
import com.csun.cmny.provider.service.UacStaffStatusService;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@Transactional
public class UacStaffStatusServiceImpl extends BaseService<UacStaffStatus> implements UacStaffStatusService {

    @Resource
    private UacStaffStatusMapper uacStaffStatusMapper;

    @Resource
    private UacStaffService uacStaffService;

    @Override
    public Integer changeStatus(LoginAuthDto loginAuthDto,Integer status) {
        //查询当前状态
        UacStaff uacStaff = uacStaffService.selectByUserId(loginAuthDto.getUserId());
        if (PublicUtil.isEmpty(uacStaff)) {
            throw new UacBizException(ErrorCodeEnum.UAC10011047);
        }
        if(uacStaff.getStatus()==status){
            if(status==1){
                throw new UacBizException(ErrorCodeEnum.UAC10011049);
            }else{
                throw new UacBizException(ErrorCodeEnum.UAC10011050);
            }
        }
        uacStaff.setStatus(status);
        uacStaffService.update(uacStaff);

        //状态修改日志
        UacStaffStatus uacStaffStatus = new UacStaffStatus();
        uacStaffStatus.setStaffId(uacStaff.getId());
        uacStaffStatus.setStatus(uacStaff.getStatus());

        return save(uacStaffStatus);
    }

    @Override
    public PageInfo listPage(StaffStatusQueryDto staffStatusQueryDto) {

        List<StaffStatusVo> staffStatusVos=uacStaffStatusMapper.listPage(staffStatusQueryDto);

        return new PageInfo<>(staffStatusVos);

    }

    @Override
    public int deleteByStaffId(Long id) {
        return uacStaffStatusMapper.deleteByStaffId(id);
    }
}
